package com.salon.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.salon.server.entity.Project;
import com.salon.server.model.R;
import com.salon.server.model.request.ProjectRequest;
import com.salon.server.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/salon/project")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    // 分页，增删改
    @PostMapping("page")
    public R page(@RequestBody ProjectRequest projectRequest) {

        return R.success(projectService.page(projectRequest, new LambdaQueryWrapper<Project>()
                .eq(projectRequest.getStatus() != null, Project::getStatus, projectRequest.getStatus())
                .like(projectRequest.getName() != null, Project::getName, projectRequest.getName())
                .orderByDesc(Project::getCreatedAt)
        ));
    }

    /**
     * 新增项目
     */
    @PostMapping("add")
    public R add(@RequestBody Project project) {
        return R.success(projectService.save(project));
    }

    /**
     * 删除项目
     */
    @PostMapping("delete")
    public R delete(@RequestBody Project project) {
        return R.success(projectService.removeById(project.getId()));
    }

    /**
     * 查询单个项目
     */
    @PostMapping("query")
    public R query(@RequestBody Project project) {
        return R.success(projectService.getById(project.getId()));
    }

    /**
     * 更新项目
     */
    @PostMapping("update")
    public R update(@RequestBody Project project) {
        return R.success(projectService.updateById(project));
    }

    /**
     * list
     */
    @PostMapping("list")
    public R list(@RequestBody ProjectRequest projectRequest) {
        return R.success(projectService.list(new LambdaQueryWrapper<Project>()
                .eq(projectRequest.getCategory() != null, Project::getCategory, projectRequest.getCategory())
                .eq(projectRequest.getStatus() != null, Project::getStatus, projectRequest.getStatus())
        ));
    }

    /**
     * 项目关联的美容师
     */
    @PostMapping("beautician")
    public R beautician(@RequestBody Project project) {

        return R.success(projectService.beautician(project));
    }

}
